package com.weblog.spider.processor;

import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 用户爬取类
 * @author MaoLin Wang
 * @date 2019/11/2419:45
 */
@Component
public class ArticleProcessor implements PageProcessor {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex("https://www.cnblogs.com/[^cmt][a-z 0-9]+/p/[0-9]{8}.html").all());
        String nickname = page.getHtml().xpath("//*[@id=\"post_list\"]/div[12]/div[2]/div/a/text()").get();
        String avatar = page.getHtml().xpath("//*[@id=\"post_list\"]/div[11]/div[2]/p/a/").css("img","src").get();

        if (nickname != null && avatar != null ) {
            page.putField("nickname", nickname);
            page.putField("avatar", avatar);
        } else {
            //跳过
            page.setSkip(true);
        }

    }

    @Override
    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }

}
